//
//  EditTodoView.swift
//  ToDo4
//
//  Created by Jeyi Zhou on 2025/10/27.
//

import SwiftUI

struct EditTodoView: View {
    @ObservedObject var todoManager: TodoManager
    let todo: TodoItem
    @Environment(\.dismiss) private var dismiss
    
    @State private var title: String
    @State private var description: String
    @State private var selectedQuadrant: Quadrant
    
    private var isFormValid: Bool {
        !title.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
    }
    
    init(todoManager: TodoManager, todo: TodoItem) {
        self.todoManager = todoManager
        self.todo = todo
        self._title = State(initialValue: todo.title)
        self._description = State(initialValue: todo.description)
        self._selectedQuadrant = State(initialValue: todo.quadrant)
    }
    
    var body: some View {
        VStack(spacing: 20) {
            // 标题栏
            HStack {
                Text("编辑任务")
                    .font(.title2)
                    .fontWeight(.bold)
                Spacer()
                Button("关闭") {
                    dismiss()
                }
                .buttonStyle(.plain)
            }
            
            Divider()
            
            // 任务标题
            VStack(alignment: .leading, spacing: 8) {
                Text("任务标题 *")
                    .font(.headline)
                TextField("请输入任务标题", text: $title)
                    .textFieldStyle(.roundedBorder)
            }
            
            // 任务描述
            VStack(alignment: .leading, spacing: 8) {
                Text("任务描述")
                    .font(.headline)
                TextEditor(text: $description)
                    .frame(height: 100)
                    .overlay(
                        RoundedRectangle(cornerRadius: 8)
                            .stroke(Color.gray.opacity(0.3), lineWidth: 1)
                    )
                    .overlay(
                        Group {
                            if description.isEmpty {
                                Text("请输入任务描述（可选）")
                                    .foregroundColor(.gray)
                                    .padding(.horizontal, 8)
                                    .padding(.vertical, 12)
                            }
                        },
                        alignment: .topLeading
                    )
            }
            
            // 四象限选择
            VStack(alignment: .leading, spacing: 12) {
                Text("选择象限 *")
                    .font(.headline)
                
                LazyVGrid(columns: Array(repeating: GridItem(.flexible()), count: 2), spacing: 12) {
                    ForEach(Quadrant.allCases, id: \.self) { quadrant in
                        QuadrantSelectionCard(
                            quadrant: quadrant,
                            isSelected: selectedQuadrant == quadrant,
                            onTap: {
                                selectedQuadrant = quadrant
                            }
                        )
                    }
                }
            }
            
            Spacer()
            
            // 按钮
            HStack(spacing: 12) {
                Button("取消") {
                    dismiss()
                }
                .buttonStyle(.bordered)
                .frame(maxWidth: .infinity)
                
                Button("保存修改") {
                    updateTodo()
                }
                .buttonStyle(.borderedProminent)
                .frame(maxWidth: .infinity)
                .disabled(!isFormValid)
            }
        }
        .padding()
        .frame(minHeight: 400)
    }
    
    private func updateTodo() {
        var updatedTodo = todo
        updatedTodo.title = title.trimmingCharacters(in: .whitespacesAndNewlines)
        updatedTodo.description = description.trimmingCharacters(in: .whitespacesAndNewlines)
        updatedTodo.quadrant = selectedQuadrant
        updatedTodo.updatedAt = Date()
        
        todoManager.updateTodo(updatedTodo)
        todoManager.showFeedback("任务已更新")
        dismiss()
    }
}

#Preview {
    EditTodoView(
        todoManager: TodoManager(),
        todo: TodoItem(title: "测试任务", description: "这是一个测试任务", quadrant: .urgentImportant)
    )
}
